<?php
/**
 * 题目管理模块
 * User: cqfut
 * Date: 2019/4/5
 * Time: 13:37
 */

require_once ("../../config/path_config.php");
include_once (ROOTDIR."common/base_router.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."admin/model/problem_model.php");

class ProblemRouter extends BaseRouter {
    public function __construct() {
        parent::__construct();
    }

    function importProblems() {
        $createrId = BaseRouter::getSession('user_id');
        // 1. 服务器会自动将接收到的数据进行URL解码,还原成base64编码
        $base64Str = $this->getReqStrVal('xml');
        $xmlStr = base64_decode($base64Str);
        $problems = new SimpleXMLElement($xmlStr);
        $problemIds = array();
        foreach( $problems as $problem ) {
            $title = $problem->title; // 题目名称
            $desc = (string)$problem->desc; // 题目描述(string)用于去除 <![CDATA[ ]]>标记
            $input = "无";
            if ( isset($problem->input) ) {
                $input = (string)$problem->input;
            }
            $output = (string)$problem->output;
            $sampleIn = "";
            if ( isset($problem->samplein) ) {
                $sampleIn = $problem->samplein;
                $sampleIn = ltrim($sampleIn);
                $sampleIn = rtrim($sampleIn);
            }
            $sampleOut = "";
            if ( isset($problem->sampleout)) {
                $sampleOut = $problem->sampleout;
                $sampleOut = ltrim($sampleOut);
                $sampleOut = rtrim($sampleOut);
            }
            $hint = "";
            if ( isset($problem->hint) ) {
                $hint = (string)$problem->hint;
            }
            $level = 1;
            if ( isset($problem->level) ) {
                $level = $problem->level;
            }
            $category = "未知";
            if ( isset($problem->category) ) {
                $category = $problem->category;
            }
            $source = "原创";
            if ( isset($problem->source) ) {
                $source = $problem->source;
            }
            $timeLimit = 1;
            $memLimit = 128;
            $model = new ProblemModel();
            $pid = $model->addProblem(
                $title, $desc, $input, $output,
                $sampleIn, $sampleOut,
                $hint, $category, $source,
                $timeLimit, $memLimit, $level, $createrId);
            array_push($problemIds, $pid);
        }
        $this->setRspData($problemIds);
    }

    public function run() {
        $op = $this->getReqVal('op');
        if ( $op == 'import') {
            $this->importProblems();
        }
        else if ( $op == 'add') {

        }
        else {
            $this->setRspStatus(0, -1,"参数错误" );
        }
    }
}

$router = new ProblemRouter();
$privilegeNames = array('administrator', 'problem_editor');
if ( ! $router->checkAuth($privilegeNames) ) {
    $router->setRspStatus(0, -1001,"不具备该权限");
    $router->finish();
    exit(1);
}
$router->init();
$router->run();
$router->finish();